""" AnyBackup is a windows specific backup program to keep multiple volumes in sync with multiple volumes
    Copyright (C) 2011  Andrew Hopkinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    """

import win32com.client


def getLetters():
    diskInfo = getDriveObjects()
    letters = {}
    for disk in diskInfo:
        letter = disk.Name
        name = str(disk.VolumeName)
        letter = letter.replace(":", "")
        letters[letter + ' - ' + name] = letter
    return letters


def exists(letter):
    letters = getLetters()
    return letter in letters.values()


def getDriveObjects():
    objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    objSWbemServices = objWMIService.ConnectServer(".", "root\cimv2")
    diskInfo = objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")
    return diskInfo


def getDriveDictionary():
    drives = getDriveObjects()
    driveDict = {}
    for drive in drives:
        driveDict[str(drive.VolumeName) + str(drive.VolumeSerialNumber)] = True
    return driveDict


def getDriveObject(letter):
    diskInfo = getDriveObjects()
    for disk in diskInfo:
        diskLetter = disk.Name
        diskLetter = diskLetter.replace(":", "")
        if diskLetter.lower() == letter.lower():
            return disk
    return None


def explodePath(dirIn):
    return dirIn.split(':')


def getLetter(volName, volSerial):
    diskInfo = getDriveObjects()
    for disk in diskInfo:
        serial = str(disk.VolumeSerialNumber)
        name = disk.VolumeName
        if name is None:
            continue
        letter = disk.Name
        letter = letter.replace(":", "")
        if volName.lower() == name.lower() and volSerial == serial:
            return letter
    return None


def getFreeSpace(letter):
    disk = getDriveObject(letter)
    if disk is not None:
        return float(disk.FreeSpace) / float(1024)
    return float(0)


def getTotalSpace(letter):
    disk = getDriveObject(letter)
    if disk is not None:
        return float(disk.Size) / float(1024)
    return float(0)


def getName(letter):
    disk = getDriveObject(letter)
    if disk is not None:
        return str(disk.VolumeName)
    return None


def getSerial(letter):
    disk = getDriveObject(letter)
    if disk is not None:
        return str(disk.VolumeSerialNumber)
    return None